package com.sinochem.yunlian.ship.base.rest.app;

import com.sinochem.yunlian.ship.api.vo.bs.BsVersionVo;
import com.sinochem.yunlian.ship.base.biz.BsVersionBiz;
import com.sinochem.yunlian.ship.base.entity.BsVersion;
import com.sinochem.yunlian.ship.base.enume.VersionType;
import com.sinochem.yunlian.ship.base.vo.response.RespBsVersionVo;
import com.sinochem.yunlian.ship.common.msg.RestResponse;
import com.sinochem.yunlian.ship.common.rest.BaseController;
import com.sinochem.yunlian.ship.common.util.VersionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Auther: wangHui
 * @Dscription: 版本更新
 * @Date: Created in 11:21 2017/12/26
 **/
@Controller
@RestController
@RequestMapping(value = "/m")
public class MBsVersionController extends BaseController<BsVersionBiz,BsVersion> {

    @Autowired
    private BsVersionBiz bsVersionBiz;

    /**
     * @Auther: wanghui
     * @Dscription: 根据 appId 获取版本信息
     * @Date: Created in 11:42 2017/12/26
     **/
    @RequestMapping(value = "/ver/versionInfo",method = RequestMethod.POST)
    public RestResponse getVersionInfoByAppId(){
        RestResponse response = new RestResponse();
        String appId = this.getCurrentAppId();
        String clientVersion = this.getCurrentAppClientVersion();
        BsVersion bsVersion = bsVersionBiz.getBsVersionByAppId(appId);
        BsVersionVo vo = new BsVersionVo();
        vo.setId(bsVersion.getId());
        vo.setAppId(bsVersion.getAppId());
        vo.setAppName(bsVersion.getAppName());
        vo.setClientVersion(bsVersion.getClientVersion());
        vo.setDownloadUrl(bsVersion.getDownloadUrl());
        vo.setRemark(bsVersion.getRemark());
        //版本Type添加
        if(VersionUtil.validateVersionUpdate(clientVersion,bsVersion.getClientVersion())){
            vo.setType(1);
            vo.setTypeName("强制升级");
        } else {
            vo.setType(2);
            vo.setTypeName("不强制升级");
        }
        vo.setUpdateId(bsVersion.getUpdateId());
        vo.setUpdateTime(bsVersion.getUpdateTime());
        response.setData(vo);
        return response;
    }

    /**
     * @Auther: wanghui
     * @Dscription: 根据 appId 获取版本信息
     * @Date: Created in 11:42 2017/12/26
     **/
    @RequestMapping(value = "/version/versionInfo/anon",method = RequestMethod.POST)
    @Deprecated
    public RestResponse getVersionInfoByAppId(@RequestBody Map<String,Object> params){
        RestResponse response = new RestResponse();
        String appId = params.get("appId").toString();
        BsVersion bsVersion = bsVersionBiz.getBsVersionByAppId(appId);
        RespBsVersionVo vo = new RespBsVersionVo();
        vo.setId(bsVersion.getId());
        vo.setAppId(bsVersion.getAppId());
        vo.setAppName(bsVersion.getAppName());
        vo.setClientVersion(bsVersion.getClientVersion());
        vo.setDownloadUrl(bsVersion.getDownloadUrl());
        vo.setRemark(bsVersion.getRemark());
        vo.setType(bsVersion.getType());
        if(bsVersion.getType() !=null){
            vo.setTypeName(VersionType.valueOf(bsVersion.getType()));
        }
        vo.setUpdateId(bsVersion.getUpdateId());
        vo.setUpdateTime(bsVersion.getUpdateTime());
        response.setData(vo);
        return response;
    }

}
